﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using System.Data;

namespace Simple.Services;

public class QHSEDangerClauseService
{
    private readonly SimpleDbContext _context;
    private readonly CacheService _cacheService;
    public QHSEDangerClauseService(SimpleDbContext context, CacheService cacheService)
    {
        _context = context;
        _cacheService = cacheService;
    }
    public async Task<List<QHSEDangerClauseModel>> GetAsync()
    {
        string name = "11";
        var models = await _cacheService.GetQHSEDangerClauseAsync(name);
        if (models.Count == 0)
        {
            var query = _context.Set<QHSESysDangerClause>().AsNoTracking().AsQueryable();
            var big =await query.Where(u => u.SpecialtyCode == null).ToListAsync();
            var specialtyViolations = await query.Join(_context.Set<QHSESysKeyWorkSpecialty>(),
                clause => clause.SpecialtyCode,
                specialty => specialty.SpecialtyCode,
                (clause, specialty) => new { Clause = clause, Specialty = specialty })
                .Select(join => new QHSESysDangerClause
                {
                    GeneralSpecialtyId = join.Clause.GeneralSpecialtyId,
                    SpecialtyCode = join.Clause.SpecialtyCode,
                    DangerTypeCode = join.Clause.DangerTypeCode,
                    Description = "(" + join.Specialty.Specialty + ")" + join.Clause.No + join.Clause.Description,
                    Note = join.Clause.Note,
                    No = join.Clause.No
                }
                ).ToListAsync();
            
            specialtyViolations.AddRange(big);
            models = MapperHelper.Map<List<QHSEDangerClauseModel>>(specialtyViolations);
            await _cacheService.SetQHSEDangerClauseAsync(models, name);
        }

        return models;
    }
    public async Task<List<QHSEDangerClauseModel>> GetByNameAsync(IdInputModel model)
    {
        var models = await _cacheService.GetQHSEDangerClauseAsync(model.Id);
        if (models.Count == 0)
        {
            var query = _context.Set<QHSESysDangerClause>().AsNoTracking().Where(u => EF.Functions.Like(u.Description, $"%{model.Id}%")).AsQueryable();
            var big = await query.Where(u => u.SpecialtyCode == null).Where(u => EF.Functions.Like(u.Description, $"%{model.Id}%")).ToListAsync();
            var specialtyViolations = await query.Join(_context.Set<QHSESysKeyWorkSpecialty>(),
                clause => clause.SpecialtyCode,
                specialty => specialty.SpecialtyCode,
                (clause, specialty) => new { Clause = clause, Specialty = specialty })
                .Select(join => new QHSESysDangerClause
                {
                    GeneralSpecialtyId = join.Clause.GeneralSpecialtyId,
                    SpecialtyCode = join.Clause.SpecialtyCode,
                    DangerTypeCode = join.Clause.DangerTypeCode,
                    Description = "(" + join.Specialty.Specialty + ")" + join.Clause.No + join.Clause.Description,
                    Note = join.Clause.Note,
                    No = join.Clause.No
                }
                ).ToListAsync();
            specialtyViolations.AddRange(big);
            models = MapperHelper.Map<List<QHSEDangerClauseModel>>(specialtyViolations);
            await _cacheService.SetQHSEDangerClauseAsync(models,model.Id);
        }

        return models;
    }
    public async Task<QHSEDangerClauseModel> GetByIdAsync(IdInputModel model)
    {
            var clause =await _context.Set<QHSESysDangerClause>().AsNoTracking().Where(u => u.GeneralSpecialtyId == model.Id).Join(_context.Set<QHSESysKeyWorkSpecialty>(),
                clause => clause.SpecialtyCode,
                specialty => specialty.SpecialtyCode,
                (clause, specialty) => new { Clause = clause, Specialty = specialty })
                .Select(join => new QHSESysDangerClause
                {
                    GeneralSpecialtyId = join.Clause.GeneralSpecialtyId,
                    SpecialtyCode = join.Clause.SpecialtyCode,
                    DangerTypeCode = join.Clause.DangerTypeCode,
                    Description = "(" + join.Specialty.Specialty + ")" + join.Clause.No + join.Clause.Description,
                    Note = join.Clause.Note,
                    No = join.Clause.No
                }
                ).FirstOrDefaultAsync();
            var models = MapperHelper.Map<QHSEDangerClauseModel>(clause);

        return models;
    }
    public async Task<PageResultModel<QHSEDangerClauseModel>> QueryByCodesAsync(SpecialtyDangerInputModel input)
    {
        var result = new PageResultModel<QHSEDangerClauseModel>();
        var query = _context.Set<QHSESysDangerClause>().AsNoTracking().AsQueryable();
        if (!string.IsNullOrEmpty(input.SpecialtyCode))
        {
            query = query.Where(u => EF.Functions.Like(u.SpecialtyCode, $"%{input.SpecialtyCode}%"));

        }
        if (!string.IsNullOrEmpty(input.DangerTypeCode))
        {
            query = query.Where(u => EF.Functions.Like(u.DangerTypeCode, $"%{input.DangerTypeCode}%"));

        }
        // 获取总数量
        result.TotalRows = await query.CountAsync();

        // 分页查询
        input.PageSize = result.TotalRows;
        query = query.Page(input.PageNo, input.PageSize);
        var specialtyDangers = await query.ToListAsync();
        result.Rows = MapperHelper.Map<List<QHSEDangerClauseModel>>(specialtyDangers);

        result.SetPage(input);
        result.CountTotalPage();

        return result;
    }

}
